/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Shell;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace DslEditorPowerToy.Controls.Editors
{
    /// <summary>
    /// A base class for editor controls
    /// </summary>
    public abstract class DslEditorControlBase : UserControl, IDslEditorControl, ISelectionService, IMonitorSelection
    {
        private ModelElement _rootElement = null;

        #region Constructors
        protected DslEditorControlBase()
            : base()
        {
        }
        #endregion

        #region ISelectionService Members

        public virtual bool GetComponentSelected(object component)
        {
            if (null != this.SelectableComponents)
                return IsComponentSelected(component);

            return false;
        }

        public virtual System.Collections.ICollection GetSelectedComponents()
        {
            return this.SelectableComponents;
        }

        public virtual object PrimarySelection
        {
            get
            {
                ICollection selectables = this.SelectableComponents;
                if (null != selectables)
                {
                    if (selectables.Count > 0)
                        return new ArrayList(selectables)[0];
                }

                return null;
            }
        }

        public event EventHandler SelectionChanged;

        public event EventHandler SelectionChanging;

        public virtual int SelectionCount
        {
            get
            {
                if (null != this.SelectableComponents)
                    return this.SelectableComponents.Count;
                else
                    return 0;
            }
        }

        public virtual void SetSelectedComponents(System.Collections.ICollection components, SelectionTypes selectionType)
        {
        }

        public virtual void SetSelectedComponents(System.Collections.ICollection components)
        {
        }

        #endregion

        #region IMonitorSelection Members
        /// <summary>
        /// Handles change in selection from host control
        /// </summary>
        /// <param name="selectionService"></param>
        /// <param name="source"></param>
        public void NotifySelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
            OnNotifySelectionChanged(selectionProvider, source);
        }

        #endregion

        #region Protected Properties
        /// <summary>
        /// Returns the list of selectable components
        /// </summary>
        /// <returns></returns>
        protected abstract ICollection SelectableComponents
        {
            get;
        }
        /// <summary>
        /// Gets the root element of the model
        /// </summary>
        protected ModelElement RootElement
        {
            get
            {
                return this._rootElement;
            }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Handles selection change
        /// </summary>
        /// <param name="selectionProvider"></param>
        /// <param name="source"></param>
        protected virtual void OnNotifySelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {

        }
        /// <summary>
        /// gets whether the specified component is selected
        /// </summary>
        protected abstract bool IsComponentSelected(object component);
        /// <summary>
        /// Raises the selection changed event
        /// </summary>
        protected void OnSelectionChanged()
        {
            if (null != this.SelectionChanged)
                this.SelectionChanged(this, new EventArgs());
        }
        /// <summary>
        /// Raises the selection changing event
        /// </summary>
        protected void OnSelectionChanging()
        {
            if (null != this.SelectionChanging)
                this.SelectionChanging(this, new EventArgs());
        }
        #endregion

        #region IDslEditorControl Members
        /// <summary>
        /// Handles change in root element of model
        /// </summary>
        /// <param name="rootElement"></param>
        public virtual void OnRootElementChanged(ModelElement rootElement)
        {
            this._rootElement = rootElement;
        }

        #endregion
    }
}
